﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QLKS
{
    public partial class frphong : Form
    {
        public frphong()
        {
            InitializeComponent();
        }

        private DataTable _dSPhg;

        internal DataTable DSPhg
        {
            get { return _dSPhg; }
            set { _dSPhg = value; }
        }
        private DataTable _dSLoaiPhg;

        internal DataTable DSLoaiPhg
        {
            get { return _dSLoaiPhg; }
            set { _dSLoaiPhg = value; }
        }
        private void frphong_Load(object sender, EventArgs e)
        {
            DSPhg = BUS.XuLyPhong.TimDSPhong();
            dgvDSPhg.DataSource = DSPhg;

            DSLoaiPhg = BUS.XuLyLoaiPhong.TimToanBoLoaiPhong();
            List<String> MaPhg = new List<string>();
            foreach (DataRow dr in DSLoaiPhg.Rows)
            {
                MaPhg.Add(dr["LoaiPhong"].ToString());
            }
            cboMaLoaiPhg.DataSource = MaPhg;
        }

        private void cboMaLoaiPhg_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow dr in DSLoaiPhg.Rows)
                {
                    if (cboMaLoaiPhg.Text == dr["LoaiPhong"].ToString())
                    {
                        txtMaLoai.Text = dr["MaLoaiPhong"].ToString();
                    }
                }
            }
            catch (System.Exception )
            {
            	
            }
           
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            try
            {
                DTO.CPHONG p = new DTO.CPHONG();
                p = BUS.XuLyPhong.TimPhongTheoMaPhong(txtMaPhg.Text);

                if (p.MaPhong == "")
                {
                    p = new DTO.CPHONG();
                    p.MaPhong = txtMaPhg.Text;
                    p.TenPhong = txtTenPhg.Text;
                    p.MaLoaiPhong = txtMaLoai.Text;
                    p.TinhTrang = 0;
                    if (!(BUS.XuLyPhong.ThemPhong(p)))
                    {
                    }
                }
                else
                    MessageBox.Show("Phong nay da ton tai.");
            }
            catch (System.Exception )
            {
            	
            }
            frphong_Load(sender, e);
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (dgvDSPhg.SelectedRows.Count > 0)
            {
                int index = dgvDSPhg.SelectedRows[0].Index;

                DataRow dr = DSPhg.Rows[index];

                DTO.CPHONG p = new DTO.CPHONG();
                p.MaLoaiPhong = txtMaLoai.Text;
                p.MaPhong = txtMaPhg.Text;
                p.TenPhong = txtTenPhg.Text;
                p.TinhTrang = 0;

                if (!(BUS.XuLyPhong.SuaTTPhong(p,dr["MaPhong"].ToString())))
                {
                    MessageBox.Show("Loi");
                }
            }
            else
            {
                MessageBox.Show("Ban chua chon phong.");
            }

            frphong_Load(sender, e);
        }

        private void buttonX3_Click(object sender, EventArgs e)
        {
            if (dgvDSPhg.SelectedRows.Count > 0)
            {
                int index = dgvDSPhg.SelectedRows[0].Index;

                DataRow dr = DSPhg.Rows[index];
                if (!(BUS.XuLyPhong.XoaPhong(dr["MaPhong"].ToString()))) 
                {

                }
            }
            else
            {
                MessageBox.Show("Ban chua chon phong.");
            }

            frphong_Load(sender, e);
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDSPhg_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = dgvDSPhg.SelectedRows[0].Index;

                DataRow dr = DSPhg.Rows[index];
                txtTenPhg.Text = dr["TenPhong"].ToString();
                txtMaPhg.Text = dr["MaPhong"].ToString();
                txtMaLoai.Text = dr["MaLoaiPhong"].ToString();
            }
            catch (System.Exception )
            {
            	
            }
            

        }

        private void txtMaLoai_TextChanged(object sender, EventArgs e)
        {
            try
            {
                foreach (DataRow row in DSLoaiPhg.Rows)
                {
                    if (row["MaLoaiPhong"].ToString() == txtMaLoai.Text)
                    {
                        cboMaLoaiPhg.Text = row["LoaiPhong"].ToString();
                    }
                }
            }
            catch (System.Exception )
            {
            	
            }
            
        }
    }
}
